package com.tourismhengyang.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.tourismhengyang.beans.LocalInfo;
import com.tourismhengyang.biz.ILocalInfoBiz;
import com.tourismhengyang.impl.LocalInfoBizImpl;
import com.tourismhengyang.utils.JsonObject;

public class LocalInfoAction extends ActionSupport implements ModelDriven<LocalInfo>{

	private static final long serialVersionUID = 1L;
	private File[] upload;//上传的文件
	private String[] uploadContentType;
	private String[] uploadFileName;
	private LocalInfo localInfo;
	private ILocalInfoBiz localInfoBiz=new LocalInfoBizImpl();
	private HttpSession session;
	private JsonObject json;
	{
		session=ServletActionContext.getRequest().getSession();
	}
	public String addLocalInfo() throws Exception{
		String realpath = ServletActionContext.getServletContext().getRealPath("//../upload");
		
		if (upload != null) {
            File savedir=new File(realpath);
            if(!savedir.getParentFile().exists())
                savedir.getParentFile().mkdirs();
            for(int i=0;i<upload.length;i++){
            	String newName=new Date().getTime()+""+new Random().nextInt(1000)+uploadFileName[i].substring(uploadFileName[i].indexOf("."));
                File savefile = new File(savedir, newName);
                FileUtils.copyFile(upload[i], savefile);
                System.out.println(uploadContentType[i]);
                if("image/png".equals(uploadContentType[i]) || "image/jpeg".equals(uploadContentType[i])){
                	localInfo.setLpic("../../upload/"+newName);
                }else{
                	localInfo.setLvideo("../../upload/"+newName);
                }
            }
            int result=localInfoBiz.addLocalInfo(localInfo);
            json=new JsonObject();
            json.setObj(result);
        }
		return SUCCESS;
	}
	
	public String getLocalInfoList(){
		List<LocalInfo> list=localInfoBiz.getLocalInfoList();
		if(list!=null){
			json=new JsonObject();
			json.setRows(list);
		}
		return SUCCESS;
	}
	
	public String getLids(){
		List<LocalInfo> list=localInfoBiz.getLids();
		if(list!=null){
			json=new JsonObject();
			json.setRows(list);
			return SUCCESS;
		}
		return null;
	}
	
	public String getInfoByLid(){
		LocalInfo oneInfo=localInfoBiz.getInfoByLid(localInfo.getLid());
		if(oneInfo!=null){
			json=new JsonObject();
			json.setObj(1);
			session.setAttribute("oneLocalInfo", oneInfo);
			return SUCCESS;
		}
		return null;
	}
	
	public String getSearchInfo(){
		List<LocalInfo> list=localInfoBiz.getSearchInfo(localInfo.getLname());
		if(list!=null){
			json=new JsonObject();
			json.setObj(1);
			session.setAttribute("searchResult", list);
			Map<String,Object> searchInfo=new HashMap<String,Object>();
			searchInfo.put("scontext", localInfo.getLname());
			searchInfo.put("result", list.size());
			session.setAttribute("searchInfo", searchInfo);
			return SUCCESS;
		}
		return null;
	}
	
	public File[] getUpload() {
		return upload;
	}
	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	
	public JsonObject getJson() {
		return json;
	}
	
	@Override
	public LocalInfo getModel() {
		if(localInfo==null){
			localInfo=new LocalInfo();
		}
		return localInfo;
	}
}
